<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>分类管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-table :data="getCategoryData" style="width: 100%" stripe border>
        <el-table-column type="index" label="#" width="width"></el-table-column>
        <el-table-column prop="_id" label="ID" width="width"></el-table-column>
        <el-table-column label="上级分类" width="width" prop="parent.name"></el-table-column>
        <el-table-column prop="name" label="分类名称" width="width"></el-table-column>
        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="editCategory(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="removeCategory(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog title="修改分类" :visible.sync="editCategoryVisible" width="50%">
      <el-form label-width="80px">
        <el-form-item label="上级分类">
          <el-select v-model="editForm.parent" placeholder value-key="valueKey">
            <el-option
              v-for="item in getCategoryData"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCategoryVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'CategoryList',
  data() {
    return {
      getCategoryData: [],
      // 编辑对话框的显示与隐藏
      editCategoryVisible: false,
      // 编辑分类绑定数据
      editForm: {
        _id: 0,
        name: '',
        parent: {}
      },
      valueKey: ''
    }
  },
  methods: {
    async getCategoryList() {
      const { data: res } = await this.$http.get(`rest/categories`)
      this.getCategoryData = res
    },
    async editCategory(row) {
      this.editCategoryVisible = true
      const res = await this.$http.get(`rest/categories/${row._id}`)
      this.editForm = res.data
      this.valueKey = String(this.editForm._id)
    },

    async editSave() {
      await this.$http.put(
        `rest/categories/${this.editForm._id}`,
        this.editForm
      )
      console.log('ok')
      this.editCategoryVisible = false
      this.$message.success(`成功更新分类：${this.editForm.name}`)
      this.getCategoryList()
    },
    async removeCategory(row) {
      const confirm = await this.$confirm(
        `是否删除分类：${row.name}?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirm === 'cancel') {
        return this.$message.info('取消删除操作')
      } else {
        const res = await this.$http.delete(`rest/categories/${row._id}`)
        this.$message.success(`成功删除分类：${res.data.name}`)
        this.getCategoryList()
      }
    }
  },
  created() {
    this.getCategoryList()
  }
}
</script>

<style scoped>
.el-card {
  margin-top: 15px;
}
</style>